package cc.rengu.igas.channel.wechat.core.service.trans;

import cc.rengu.igas.channel.wechat.common.constant.WechatParamConstant;
import cc.rengu.igas.channel.wechat.core.model.ChnlBizResponse;
import cc.rengu.igas.channel.wechat.core.service.base.NoticeService;
import cc.rengu.igas.channel.wechat.facade.request.CupsWechatPayNoticeRequest;
import cc.rengu.igas.share.facade.base.Header;
import cc.rengu.igas.share.facade.request.WechatPayResultNoticeRequest;
import cc.rengu.igas.share.facade.response.WechatPayResultNoticeResponse;
import cc.rengu.igas.share.facade.result.Result;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.enums.ChannelEnum;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.oltp.utility.util.*;
import com.alibaba.fastjson.JSON;

public class CupsWechatPayResultNoticeService extends NoticeService {
    @Override
    protected Object beforeBizProcess() throws Exception {
        CupsWechatPayNoticeRequest cupsWechatPayNoticeRequest = new CupsWechatPayNoticeRequest();
        ConvertUtil.convertOutput(cupsWechatPayNoticeRequest);
        return cupsWechatPayNoticeRequest;
    }

    @Override
    protected ChnlBizResponse callBizService(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        WechatPayResultNoticeRequest wechatPayResultNoticeRequest = new WechatPayResultNoticeRequest();
        ConvertUtil.convertOutput(wechatPayResultNoticeRequest);
        Header header = wechatPayResultNoticeRequest.getHeader();
        header.setTxnNum("T00SH002");
        header.setTraceNo(ChannelEnum.UPWX.getChannelType() + DateUtil.getCurrentDateTime("yyyyMMddHHmmssSSS") + RandomUtil.randomNumStr(10));
        header.setSrcSysId(ChannelEnum.UPWX.getChannelType());
        header.setChanlId("*");
        wechatPayResultNoticeRequest.setHeader(header);
        rglog.debug("打印微信交易结果通知报文<{}>", JSON.toJSONString(wechatPayResultNoticeRequest));
        DubboService dubboService = new DubboServiceImpl();
        Result<WechatPayResultNoticeResponse> responseResult = (Result<WechatPayResultNoticeResponse>) dubboService.callDubboService("alwxUnifiedService", "wechatPayResultNotice", wechatPayResultNoticeRequest);
        if (null == responseResult || !responseResult.isSuccess() || !"00000000".equals(responseResult.getResult().getRespCode())) {
            rglog.error("支付结果处理失败");
            xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, WechatParamConstant.STRING_SUCCESS);
        } else {
            xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, WechatParamConstant.STRING_SUCCESS);
        }
        xmlTreeUtil.setXmlTreeStringValue("returnCode", WechatParamConstant.STRING_SUCCESS);
        return null;
    }

    @Override
    protected void afterBizProcess(ChnlBizResponse response) throws Exception {

    }
}
